package com.gopay.common.util;

import java.math.BigDecimal;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

import org.apache.log4j.Logger;

public class NumberUtils {

	private static Logger logger = Logger.getLogger(NumberUtils.class);

	private static final BigDecimal ZERO = new BigDecimal("0");

	/**
	 * 新建0值
	 * 
	 * @return
	 */
	public static BigDecimal getZero() {
		return ZERO;
	}

	/**
	 * 新建值 num
	 * 
	 * @return
	 */
	public static BigDecimal getNum(String num) {
		try {
			return new BigDecimal(num);
		} catch (Exception e) {
			logger.error("input value : num = " + num, e);
			throw new IllegalArgumentException(
					"Cannot create BigDecimal Object with Initial String "
							+ num);
		}
	}

	/**
	 * 返回两位小数点数字字符串， 四舍五入
	 * 
	 * @param num
	 * @return
	 */
	public static String format2PointWithHalfUp(BigDecimal num) {
		if (num == null) {
            throw new NullPointerException("Param num cannot be NULL");
        }

		return num.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
	}

	/**
	 * a > b
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static boolean isLargerThan(BigDecimal a, BigDecimal b) {
		return a.compareTo(b) == 1;
	}

	/**
	 * a>= b
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static boolean isLargerEqual(BigDecimal a, BigDecimal b) {
		return a.compareTo(b) >= 0;
	}

	/**
	 * a = b
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static boolean isEqualTo(BigDecimal a, BigDecimal b) {
		return a.compareTo(b) == 0;
	}

	/**
	 * @param account 转换格式为1,000.345 格式的小数
	 * @return
	 * @throws Exception
	 */
	public static String formatNum(String account){
		Number num = null;
    	try{
    		DecimalFormatSymbols dfs = new DecimalFormatSymbols();  
    		dfs.setDecimalSeparator('.');  
    		dfs.setGroupingSeparator(',');  
    		dfs.setMonetaryDecimalSeparator('.');  
    		DecimalFormat df = new DecimalFormat("###,###.##",dfs);   
    		num = df.parse(account);
        }catch(Exception e){
        	logger.error("input value : num = " + account, e);
        }
        return num.toString();
    }
}
